define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'auth/admin/index',
                    add_url: 'auth/admin/add',
                    edit_url: 'auth/admin/edit',
                    del_url: 'auth/admin/del',
                    multi_url: 'auth/admin/multi',
                }
            });

            var table = $("#table");

            //在表格内容渲染完成后回调的事件
            table.on('post-body.bs.table', function (e, json) {
                $("tbody tr[data-index]", this).each(function () {
                    if (parseInt($("td:eq(1)", this).text()) == Config.admin.id) {
                        $("input[type=checkbox]", this).prop("disabled", true);
                    }
                });
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                columns: [
                    [
                        {field: 'state', checkbox: true, },
                          {field: 'id', title: __('Id'), sortable: true,visible:false,operate:false},
                        {field: 'username', title: __('Username')},
                        {field: 'nickname', title: __('姓名')},
                        {field: 'groups_text', title: __('岗位'), operate:false, formatter: Table.api.formatter.label},
                        {
                            field: 'sex',
                            title: __('性别'),
                            searchList: {"1": __('男'), "2": __('女')},
                            formatter: Table.api.formatter.status
                        },
                        {field: 'mobile', title: __('Mobile')},
                        {field: 'birthday', title: __('出生年月')},
                        {field: 'age', title: __('年龄')},
                        {field: 'education.education_name', title: __('最高学历')},
                        {field: 'entry_time', title: __('入职时间')},
                        {field: 'entry_day', title: __('入职天数')},
                        {field: 'status', title: __("在职状态"), searchList: {"normal":__('在职'),"hidden":__('离职')}, formatter: Table.api.formatter.status},
                        {field: 'logintime', title: __('Login time'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'update_info',
                                    text: __('查看学历信息'),
                                    title: __('查看学历信息'),
                                    classname: 'btn btn-xs btn-warning btn-dialog',
                                    url: 'auth/admin/update_info'
                                },
                                {
                                    name: 'leave',
                                    text: __('离职'),
                                    title: __('离职'),
                                    classname: 'btn btn-xs btn-warning btn-dialog',
                                    url: 'auth/admin/leave'
                                },
                            ],
                            formatter: function (value, row, index) {
                                if(row.id == Config.admin.id){
                                    return '';
                                }
                                return Table.api.formatter.operate.call(this, value, row, index);
                            }}

                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Form.api.bindevent($("form[role=form]"));
            //选择完课程后，对应的时间，天数计算方案赋值
            $("#birthday").on('changed', function(){
                //改变所属分类，获取分类描述操作
                var start_time = $(this).val();//获取选择的分类ID
                Fast.api.ajax({
                    url:'reception/sheet/getAges?start_time=' + start_time,
                    loading:false,
                    data:{},
                    success: function (data, ret) {
                        $('#age').val(data.msg.age);
                        return true;
                    }
                });



                //console.log(optid);
            });
        },
        update_info: function () {
            Form.api.bindevent($("form[role=form]"));
            addExpand();

            function addExpand() {
                var imgs = document.getElementsByTagName("img");
                imgs[0].focus();
                for(var i = 0;i<imgs.length;i++){
                    imgs[i].onclick = expandPhoto;
                    imgs[i].onkeydown = expandPhoto;
                }
            }

            function expandPhoto(){
                var overlay = document.createElement("div");
                overlay.setAttribute("id","overlay");
                overlay.setAttribute("class","overlay");
                document.body.appendChild(overlay);

                var img = document.createElement("img");
                img.setAttribute("id","expand")
                img.setAttribute("class","overlayimg");
                img.src = this.getAttribute("src");
                document.getElementById("overlay").appendChild(img);

                img.onclick = restore;
            }

            function restore(){
                document.body.removeChild(document.getElementById("overlay"));
                document.body.removeChild(document.getElementById("expand"));
            }
        },
        leave: function () {
            Form.api.bindevent($("form[role=form]"));
            var admin_id = Config.admin_id;
            $("input[type=radio][name='row[is_lz]']").change(function() {
                var is_lz = $(this).val();
                if(is_lz == 1){
                    Fast.api.ajax({
                        url: 'auth/admin/getClassInfo?admin_id=' + admin_id,
                        loading: false,
                        data: {},
                        success: function (data, ret) {
                            //这里ret.msg 是未定义的。 data里的数据需要 data.data.*来使用
                            //var description = data.description；
                            console.log(data.msg);
                            if(data.msg.is_class==1) {
                                $('.is_lz').hide();
                                $('.is_most').show();
                            }else{
                                $('.is_lz').show();
                                $('.is_most').hide();
                            }
                            //   c_days = data.msg.day;
                            // if (data.msg.no_pay_num > 0) {
                            //     $('.is_see').show();
                            // } else {
                            //     $('.is_see').hide();
                            // }
                            // $('#return_price').val(data.msg.return_price);
                            //  Toastr.success('3232');
                            return true;
                        }
                    });
                }else if(is_lz == 0){
                    $('.is_lz').hide();
                }else{
                    $('.is_show').show();
                    $('.is_two').hide();
                }
            });
            $(document).on('click', '.is_most', function (row) { //上传数据
                Fast.api.open('auth/admin/class_info?admin_id=' + admin_id, __("带班信息"), {area: ['80%', '85%']}, {
                    callback: function (value) {
                        console.log(value);
                    }
                });
            });
        },
        edit: function () {
            Form.api.bindevent($("form[role=form]"));
            $(document).on('click', '#reset_pas', function (row) { //上传数据
               $('#password').val('123456');
               Toastr.success('重置成功,密码123456');
            });
            $("#birthday").on('changed', function(){
                //改变所属分类，获取分类描述操作
                var start_time = $(this).val();//获取选择的分类ID
                Fast.api.ajax({
                    url:'reception/sheet/getAges?start_time=' + start_time,
                    loading:false,
                    data:{},
                    success: function (data, ret) {
                        $('#age').val(data.msg.age);
                        return true;
                    }
                });
                //console.log(optid);
            });
        }
    };
    return Controller;
});
